package com.nuaa.qianru.run;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nuaa.qianru.model.Tp;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.formula.functions.T;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Demo {


    /**
     * 返回成功状态码
     */
    public static void main(String[] args) throws IOException {
        JSONObject jsonObject = JSON.parseObject("{\"flag\":true,\"palletData\":\"[{\\\"PalletID\\\":\\\"TP-07\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":\\\"4FL-10_0-015G40-211-66\\\",\\\"FixtureID\\\":\\\"[\\\\\\\"GZ-4FL-10_0-015-G40-001\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-13\\\",\\\"Pos\\\":\\\"NC4\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-30\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-29\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-25\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-02\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"GZ006-ZY\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-06\\\",\\\"Pos\\\":\\\"NC1\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"nanghang1-3\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-31\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-12\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[]\\\"},{\\\"PalletID\\\":\\\"TP-28\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-20\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"shiyan-04\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-10\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"nanhang2-3\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-03\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"nanhang3-2\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-19\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"shiyan-02\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-21\\\",\\\"Pos\\\":\\\"LZ1\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-23\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-27\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-15\\\",\\\"Pos\\\":\\\"LZ2\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"\\\"},{\\\"PalletID\\\":\\\"TP-22\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":null},{\\\"PalletID\\\":\\\"TP-01\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"CESHI5\\\\\\\",\\\\\\\"CESHI2\\\\\\\",\\\\\\\"CESHI3\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-05\\\",\\\"Pos\\\":\\\"LK\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"TC4-shiyan\\\\\\\"]\\\"},{\\\"PalletID\\\":\\\"TP-04\\\",\\\"Pos\\\":\\\"NC3\\\",\\\"PalletState\\\":1,\\\"MaterialID\\\":null,\\\"FixtureID\\\":\\\"[\\\\\\\"shiyan-01\\\\\\\",\\\\\\\"nanhang1-1\\\\\\\"]\\\"}]\"}");
        System.out.println( jsonObject.get("palletData"));
        JSONArray jsonArray=jsonObject.getJSONArray("palletData");
        List<Tp> list=new ArrayList<>();
        System.out.println(jsonArray);
        for(int i=0;i<jsonArray.size();i++){
            Tp tp=new Tp();
            String id=jsonArray.getJSONObject(i).getString("PalletID");
            System.out.println(id);
            int pid=Integer.parseInt(id.substring(3));
            String pos=String.valueOf(pid);
            if(pid<17){
                pos+="01";
            }else{
                pos+="02";
            }
            String deviceid=jsonArray.getJSONObject(i).getString("Pos");
            int used=Integer.parseInt(jsonArray.getJSONObject(i).getString("PalletState"));
            tp.setDeviceId(deviceid);
            tp.setId(id);
            tp.setIsUsed(used);
            tp.setPositionId(pos);
            tp.setIndex(pid);
            list.add(tp);
        }

    }
    public static List<Tp>  get(String url) throws IOException {
        CloseableHttpClient  client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        JSONArray json = null;
        List<Tp> list=new ArrayList<>();
        try {
            HttpResponse res = client.execute(get);
            HttpEntity entity = res.getEntity();
            String str= EntityUtils.toString(entity,"Unicode");
            JSONObject jsonObject = JSON.parseObject(str);
            JSONArray jsonArray=jsonObject.getJSONArray("palletData");
            for(int i=0;i<jsonArray.size();i++){
                Tp tp=new Tp();
                String id=jsonArray.getJSONObject(i).getString("PalletID");
                System.out.println();
                int pid=Integer.parseInt(id.substring(3));
                String pos=String.valueOf(pid);
                if(pid<17){
                    pos+="01";
                }else{
                    pos+="02";
                }
                String deviceid=jsonArray.getJSONObject(i).getString("Pos");
                String itemSN = jsonArray.getJSONObject(i).getString("ItemSN");
                int used=Integer.parseInt(jsonArray.getJSONObject(i).getString("PalletState"));
                tp.setDeviceId(deviceid);
                tp.setId(id);
                tp.setIsUsed(used);
                tp.setPositionId(pos);
                tp.setIndex(pid);
                tp.setItemSn(itemSN);
                list.add(tp);
            }
            //Sort sort = JSONObject.parseObject(js,Sort.class);
            // System.out.println(sort.toString());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("11");
        } finally{
            //关闭连接 ,释放资源  
            client.close();
        }
        return list;
    }

    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }




}
